<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Vue检测数据改变的原理_数组</title>
    <script src="../js/vue.js"></script>
</head>

<body>
    <div id="root">
        <button @click="addLeader">添加校长</button>
        <h2>学校名称：{{school.name}}</h2>
        <h2>学校地址：{{school.address}}</h2>
        <h2>校长是：{{school.leader}}</h2>
        <hr />
        <button @click="addSex">添加性别</button>
        <h2>学生姓名：{{student.name}}</h2>
        <h2>学生年龄：真实{{student.age.rage}}，对外{{student.age.sage}}</h2>
        <h2>学生性别：{{student.sex}}</h2>
        <hr />
        <h2>爱好</h2>
        <ul>
            <li v-for="(h,index) in student.hobby" :key="index">
                {{h}}
            </li>
        </ul>
        <h2>朋友们</h2>
        <ul>
            <li v-for="(f, index) in student.friends" :key="index">
                {{f.name}}--{{f.age}}
            </li>
        </ul>
    </div>

    <script>
        Vue.config.productionTip = false

        const vm = new Vue({
            el: '#root',
            data: {
                school: {
                    name: '尚硅谷',
                    address: '北京'
                },
                student: {
                    name: 'tom',
                    age: {
                        rage: 40,
                        sage: 18
                    },
                    hobby: ['抽烟', '喝酒', '烫头'],
                    friends: [
                        { name: 'jack', age: 20 },
                        { name: 'tone', age: 30 }
                    ]
                }
            },
            methods: {
                addSex() {
                    this.$set(this.student, 'sex', '男')
                },
                addLeader() {
                    this.$set(this.school, 'leader', '男老师')
                }
            }
        })
        console.log(vm)
    </script>
</body>

</html>